/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package treeship.core.ship.space.impl;

import treeship.core.ship.space.Position;
import treeship.core.ship.space.Shape;

/**
 *
 * @author moonchild
 */
public class Shape2D extends Shape {

    private double width;
    private double height;
//    public Shape2D(double x, double y) {
//        this.width = x;
//        this.height = y;
//    }
    public double getWidth() {
        return width;
    }

    public double getHeight() {
        return height;
    }

    @Override
    public boolean fitsIn(Shape s) {

        if (!dimensionFitsInOtherFreespace(s)) {
            return false;
        }
        Shape2D s2d = (Shape2D) s;
        if (width > s2d.getWidth()) {
            return false;
        }
        if (height > s2d.getHeight()) {
            return false;
        }
        return true;
    }

    protected void setWidth(double width) {
        this.width = width;
    }

    protected void setHeight(double height) {
        this.height = height;
    }

    @Override
    public boolean contains(Position p) {
        Position2D p2 = (Position2D) p;
        if (p2.getX() <= width && p2.getY() <= height) {
            return true;
        }
        return false;
    }

    @Override
    public Position adjustPositionForInnerShape(Position newPos, Shape shape) {
        Position2D p1 = (Position2D) newPos;
        Shape2D shape1 = (Shape2D) shape;
        double newx;
        double newy;
        if (shape1.getWidth() + newPos.getX() > width) {
            newx = width - shape1.getWidth();
        } else {
            newx = p1.getX();
        }

        if (shape1.getHeight() + newPos.getY() > height) {
            newy = height - shape1.getHeight();
        } else {
            newy = p1.getY();
        }

        return new Position2D(newx, newy);
    }
}
